import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.ListContainersCmd;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;

import java.io.IOException;
import java.util.Arrays;

public class DockerJavaExample {
    public static void main(String[] args) {
        // 配置 Docker 客户端
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("tcp://10.82.14.78:2375") // 根据你的 Docker 守护进程地址进行配置
                .build();

        DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();
        CreateContainerCmd nginx = dockerClient.createContainerCmd("nginx");
        // 列出所有容器
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        for (Container container : listContainersCmd.exec()) {
            System.out.println("Container ID: " + container.getId());
            System.out.println("Container Name: " + Arrays.toString(container.getNames()));
            System.out.println("Image: " + container.getImage());
            System.out.println("-----------------------------");
        }

        // 关闭 Docker 客户端
        try {
            dockerClient.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}